﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using codefile;

namespace ArleneBarandGrill
{
    public partial class Form2 : Form
    {
        DataTable DTbl = new DataTable();
        Data ms = new Data();
        ProductList prod = new ProductList();

        int curIndex;

        public Form2()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
            btnAddRec.Click += new EventHandler(btnAddRec_Click);
            btnUpdateRec.Click += new EventHandler(btnUpdateRec_Click);
            txtSearch.TextChanged += new EventHandler(txtSearch_TextChanged);
            DGV.DoubleClick += new EventHandler(DGV_DoubleClick);
        }

        void DGV_DoubleClick(object sender, EventArgs e)
        {
            curIndex = DGV.SelectedRows[0].Index;
            txtProdID.Text = ms.DTbl.Rows[curIndex][0].ToString();
            txtProdName.Text = ms.DTbl.Rows[curIndex][1].ToString();
            txtProdQty.Text = ms.DTbl.Rows[curIndex][2].ToString();
            txtProdPrice.Text = ms.DTbl.Rows[curIndex][3].ToString();
        }

        void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text != "")
            {
                ms.connect();
                string sql = "SELECT * FROM tblproducts WHERE fID LIKE '%" + txtSearch.Text + "%' OR fProductName LIKE '%" + txtSearch.Text + "%' OR fProductQty LIKE '%" + txtSearch.Text + "%' OR fProductPrice LIKE '%" + txtSearch.Text + "%'";
                ms.querry(sql);
                DTbl = ms.DTbl;
                DGV.DataSource = DTbl;
                ms.close();
            }
        }

        void btnUpdateRec_Click(object sender, EventArgs e)
        {
            ProductList prodlist = new ProductList();

            prodlist.fID = txtProdID.Text;
            prodlist.fProductName = txtProdName.Text;
            prodlist.fProductQty = txtProdQty.Text;
            prodlist.fProductPrice = txtProdPrice.Text;

            ms.editprod(prodlist, curIndex);
            Form1_Load(null, null);
        }

        void btnAddRec_Click(object sender, EventArgs e)
        {
            prod.fID = txtProdID.Text;
            prod.fProductName = txtProdName.Text;
            prod.fProductQty = txtProdQty.Text;
            prod.fProductPrice = txtProdPrice.Text;

            ms.addproduct(prod);

            MessageBox.Show("Success!!!");

            txtProdID.Text = "";
            txtProdName.Text = "";
            txtProdQty.Text = "";
            txtProdPrice.Text = "";
        }

        void Form1_Load(object sender, EventArgs e)
        {
            ms.connect();
            ms.querry("SELECT * FROM tblproducts");
            DTbl = ms.DTbl;
            DGV.DataSource = DTbl;
            ms.close();
        }
    }
}
